<%@page import="java.util.UUID"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/pages/common/taglib.jsp"%>
<%
	String uuid = UUID.randomUUID().toString();
	request.setAttribute("uuid", uuid);
%>
<div class="row">
	<div class="col-md-5">
		<div class="box box-primary">
			<div class="box-header with-border" id="wordNumberTb">
				<h3 class="box-title"></h3>
				<div class="box-tools pull-right">

					<button class="btn btn-sm btn-success addBtn">
						<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
						添加
					</button>
					<button class="btn btn-sm btn-primary editBtn">
						<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
						修改
					</button>
					<button class="btn btn-sm btn-danger delBtn">
						<span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
						删除
					</button>
				</div>
			</div>
			<div class="box-body">
				<table id="workNumberGrid_${uuid}"></table>
			</div>
		</div>
	</div>
	<div class="col-md-7">
		<div class="box box-primary">
			<div class="box-header with-border" id="wordNumberEleTb">
				<h3 class="box-title"></h3>
				<div class="box-tools pull-right">
					<select name="type">
						<pf:dataDic var="item" dicCode="WORDNUMBER_ELE_TYPE">
							<option value="${item.value}">${item.name}</option>
						</pf:dataDic>
					</select>
					<button class="btn btn-sm btn-success addBtn">
						<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
						添加
					</button>

					<button class="btn btn-sm btn-primary editBtn">
						<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
						修改
					</button>
					<button class="btn btn-sm btn-danger delBtn">
						<span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
						删除
					</button>
				</div>
			</div>
			<div class="box-body">
				<table id="workNumberEleGrid_${uuid}"></table>
			</div>
		</div>
	</div>
</div>

<script type="text/javascript">
	$(function() {
		var uuid = '${uuid}';

		var typeMap = {
			type_1 : {//字符串
				handle : function(params, callback, scope) {
					params = params || {};
					var t = [];
					if (params.id) {
						t.push("id=" + params.id);
					}
					if (params.wnId) {
						t.push("wnId=" + params.wnId);
					}

					var sub = top.webUtils.topicSubscribe(
							"wordNumberEle.edit.success", function(args) {
								dialog.close();
								if (callback) {
									callback.call(scope);
								}
							});
					var dialog = webUtils.dialog({
						title : '编辑字符',
						url : ctx + '/wordNumberAct/editEleForType1.do?'+ t.join("&"),
						onremove : function() {
							sub.remove();
						}
					});
				}
			},
			type_2 : {//序列
				handle : function(params, callback, scope) {
					params = params || {};
					var t = [];
					if (params.id) {
						t.push("id=" + params.id);
					}
					if (params.wnId) {
						t.push("wnId=" + params.wnId);
					}

					var sub = top.webUtils.topicSubscribe(
							"wordNumberEle.edit.success", function(args) {
								dialog.close();
								if (callback) {
									callback.call(scope);
								}
							});
					var dialog = webUtils.dialog({
						title : '编辑序列',
						url : ctx + '/wordNumberAct/editEleForType2.do?'+ t.join("&"),
						onremove : function() {
							sub.remove();
						}
					});
				},
				refresh : function(datas, callback, scope) {

				}
			},
			type_3 : {
				handle : function(params, callback, scope) {
					params = params || {};
					var t = [];
					if (params.id) {
						t.push("id=" + params.id);
					}
					if (params.wnId) {
						t.push("wnId=" + params.wnId);
					}

					var sub = top.webUtils.topicSubscribe(
							"wordNumberEle.edit.success", function(args) {
								dialog.close();
								if (callback) {
									callback.call(scope);
								}
							});
					var dialog = webUtils.dialog({
						title : '编辑日期',
						url :ctx + '/wordNumberAct/editEleForType3.do?'+ t.join("&"),
						onremove : function() {
							sub.remove();
						}
					});
				}
			},
			type_4 : {
				handle : function(params, callback, scope) {
					if (params.id) {
						alert("该类不可编辑");
						return false;
					}
					$.post(ctx + "/wordNumberAct/saveWordNumEle.action", {
						wnId : params.wnId,
						type : 4,
						reg : "行政区划代码"
					}, function(data) {
						if (callback) {
							callback.call(scope);
						}
					}, "json");
				}
			},
			type_5 : {
				handle : function(params, callback, scope) {
					if (params.id) {
						alert("该类不可编辑");
						return false;
					}
					$.post(ctx + "/wordNumberAct/saveWordNumEle.action", {
						wnId : params.wnId,
						type : 5,
						reg : "组织机构代码"
					}, function(data) {
						if (callback) {
							callback.call(scope);
						}
					}, "json");
				}
			},
			type_6 : {
				handle : function(params, callback, scope) {
					params = params || {};
					var t = [];
					if (params.id) {
						t.push("id=" + params.id);
					}
					if (params.wnId) {
						t.push("wnId=" + params.wnId);
					}

					var sub = top.webUtils.topicSubscribe(
							"wordNumberEle.edit.success", function(args) {
								dialog.close();
								if (callback) {
									callback.call(scope);
								}
							});
					var dialog = webUtils.dialog({
						title : '编辑序列',
						url :ctx + '/wordNumberAct/editEleForType6.do?'+ t.join("&"),
						onremove : function() {
							sub.remove();
						}
					});
				}
			}
		};

		var workNumberGrid = $("#workNumberGrid_" + uuid).bootstrapTable({
			url : ctx + "/wordNumberAct/loadWordNumGrid.do",
			queryParams : function(d) {
				return d;
			},
			columns : [ {
				field : '_s',
				checkbox : true
			}, {
				title : '名称',
				field : 'title',
				align : 'left',
				width : 80
			}, {
				title : '编号',
				field : 'code',
				align : 'left',
				width : 80
			} ],
			singleSelect : true,
			onClickRow : function(row) {
				var id = row.id;
				workNumberEleGrid.wnId = id;
				workNumberEleGrid.bootstrapTable("refresh");
			}
		});

		var wordNumberTb = $("#wordNumberTb");
		wordNumberTb.find(".addBtn").click(
				function() {
					var sub = top.webUtils.topicSubscribe(
							"wordNumber.edit.success", function(args) {
								workNumberGrid.bootstrapTable("refresh");
								dialog.close();
							});
					var dialog = editWordNumber({
						onClose : function() {
							sub.remove();
						}
					});
				});

		wordNumberTb.find(".editBtn").click(
				function() {
					var rows = workNumberGrid.bootstrapTable("getSelections");
					if (rows && rows.length > 0) {

						var sub = top.webUtils.topicSubscribe(
								"wordNumber.edit.success", function(args) {
									dialog.close();
									workNumberGrid.bootstrapTable("refresh");
								});
						var row = rows[0];
						var dialog = editWordNumber({
							id : row.id,
							onClose : function() {
								sub.remove();
							}
						});
					} else {
						webUtils.alert("请选择需要编辑的数据");
					}
				});

		wordNumberTb.find(".delBtn").click(function() {
			var rows = workNumberGrid.bootstrapTable("getSelections");
			if (rows && rows.length > 0) {
				var ids = [];
				$.each(rows, function(i, n) {
					ids.push(n.id);
				});

				$.post(ctx + "/wordNumberAct/delByIds.do", {
					ids : ids.join(",")
				}, function(data) {
					if (data.success) {
						workNumberGrid.bootstrapTable("refresh");
					} else {
						webUtils.alert(data.msg);
					}
				}, 'json');
			} else {
				webUtils.alert("请选择需要编辑的数据");
			}
		});

		var editWordNumber = function(params) {
			params = params || {};
			var t = [];
			if (params.id) {
				t.push("id=" + params.id);
			}
			var dialog = webUtils.dialog({
				title : '编辑文号序列',
				url : $('<div></div>').load(ctx + '/wordNumberAct/editWordNumberView.do?'+ t.join("&")),
				onremove : function() {
					if (params.onClose) {
						params.onClose.call(null);
					}
				}
			});
			return dialog;
		}

		//---------
		var workNumberEleGrid = $("#workNumberEleGrid_" + uuid)
				.bootstrapTable(
						{
							url : ctx + "/wordNumberAct/loadWordNumEleGrid.do",
							queryParams : function(d) {
								if (workNumberEleGrid) {
									if (workNumberEleGrid.wnId) {
										d.wnId = workNumberEleGrid.wnId;
									}

								}
								return d;
							},
							columns : [
									{
										field : '_s',
										checkbox : true
									},
									{
										title : '类型',
										field : 'type',
										align : 'left',
										width : "80px",
										formatter : function(value, row, index) {
											return webUtils.getDataDicItemVal(
													"WORDNUMBER_ELE_TYPE",
													value).name;
										}
									},
									{
										title : '表达式',
										field : 'reg',
										align : 'left'
									},
									{
										title : '排序',
										field : 'sn',
										formatter : function(value, row, index) {
											var html = '<a class="btn btn-link btn-sm _j_upBt" data-id="'+row.id+'" data-index="'+value+'">'
													+ '<span class="glyphicon glyphicon-chevron-up"></span>'
													+ '</a>'
													+ '<a class="btn btn-link btn-sm _j_downBt"  data-id="'+row.id+'" data-index="'+value+'">'
													+ '<span class="glyphicon glyphicon glyphicon-chevron-down"></span>'
													+ '</a>';
											return html;
										}
									} ],
							onLoadSuccess : function() {
								 $("#workNumberEleGrid_" + uuid).find("._j_upBt").click(function(){
									 var id=$(this).data("id");
									 var index=$(this).data("index");
									 sort(id,index,"goUp");
								 });
								 $("#workNumberEleGrid_" + uuid).find("._j_downBt").click(function(){
									 var id=$(this).data("id");
									 var index=$(this).data("index");
									 sort(id,index,"goDown");
								 });
							}
						});

		function sort(id,index,action){
			$.post(ctx+"/wordNumberAct/sortElements.action",
					{id:id,action:action,sn:index},
					function(data){
						if(data.success){
							workNumberEleGrid.bootstrapTable("refresh");
						}else{
							webUtils.alert(data.msg);
						}
					},
					"json"
				);
		}
		
		var wordNumberEleTb = $("#wordNumberEleTb");

		wordNumberEleTb.find(".addBtn").click(function() {
			editWordNumberEle();
		});

		wordNumberEleTb.find(".editBtn").click(function() {
			var rows = workNumberEleGrid.bootstrapTable("getSelections");
			if (rows && rows.length > 0) {
				var row = rows[0];
				editWordNumberEle({
					id : row.id,
					type : row.type
				});
			} else {
				webUtils.alert("请选择需要编辑的数据");
			}
		});
		wordNumberEleTb.find(".delBtn").click(function() {
			var rows = workNumberEleGrid.bootstrapTable("getSelections");
			if (rows && rows.length > 0) {
				var ids = [];
				$.each(rows, function(i, n) {
					ids.push(n.id);
				});

				$.post(ctx + "/wordNumberAct/delEleByIds.do", {
					ids : ids.join(",")
				}, function(data) {
					if (data.success) {
						workNumberEleGrid.bootstrapTable("refresh");
					} else {
						webUtils.alert(data.msg);
					}
				}, 'json');
			} else {
				webUtils.alert("请选择需要编辑的数据");
			}
		});
		var editWordNumberEle = function(params) {
			params = params || {};
			var t = [];
			if (params.id) {
				t.push("id=" + params.id);
			}
			var type = params.type ? params.type : wordNumberEleTb.find(
					"select[name=type]").val();
			typeMap["type_" + type].handle({
				wnId : workNumberEleGrid.wnId,
				id : params.id
			}, function(obj) {
				workNumberEleGrid.bootstrapTable("refresh");
			}, this);
		}

	});
</script>